<template>
  <el-dialog @close="reset" :title="roleData.id>0?'编辑':'新增'" :visible.sync="info.isShow">
    <el-form ref="roleForm" label-width="80px" :model="roleData" :rules="rules">
      <el-form-item label="角色名称" prop="rolename">
        <el-input placeholder="角色名称" v-model.trim="roleData.rolename" />
      </el-form-item>
      <el-form-item label="角色权限" prop="menus">
        <!--show-checkbox: 是否显示节点名称前的复选框
            node-key: 指定树形控件内部v-for需要的key属性的属性值
            default-checked-keys: 指定默认需要被勾选的节点(数组)   
            default-expand-all: 默认展开所有的节点 
           -->
        <el-tree
          ref="tree"
          :data="menuList"
          show-checkbox
          node-key="id"
          default-expand-all
          :props="defaultProps"
          :default-checked-keys="checkedKeys"
          :check-strictly="true"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="角色状态" prop="status">
        <el-radio-group v-model="roleData.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div class="dialog-footer" slot="footer">
      <el-button @click="reset">取消</el-button>
      <el-button @click="submit" type="primary">提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  computed: {
    ...mapState("menu", ["menuList"]),
  },
  methods: {
    ...mapActions("menu", ["getMenuListAction"]),
    ...mapActions("role", ["getRoleListAction"]),
    // 更新和表单进行绑定的数据
    setFormData(obj) {
      this.roleData = { ...obj };
      //   更新树形控件默认需要勾选的节点
      this.checkedKeys = obj.menus.split(",");
    },
    // 数据重置
    reset() {
      //  关闭对话框组件
      this.info.isShow = false;
      // 清空表单元素
      this.roleData = {
        id: "",
        rolename: "",
        menus: "",
        status: "",
      };
      // 清空树形控件默认勾选节点
      this.$refs.tree.setCheckedKeys([]);
    },
    submit() {
      this.$refs.roleForm.validate((valid) => {
        if (valid) {
          console.log(this.$refs.tree.getCheckedNodes());
          const checkedKeys=[];
          this.$refs.tree.getCheckedNodes().forEach(item=>{
            checkedKeys.push(item.id);
            if(!checkedKeys.includes(item.pid)){
              checkedKeys.push(item.pid);
            }
          });
          // 提交数据
          // 手动获取树形控件中用户选择的节点id
          // this.roleData.menus = this.$refs.tree.getCheckedKeys().join(",");
          this.roleData.menus = checkedKeys.join(",");
          const url = this.roleData.id > 0 ? "/roleedit" : "/roleadd";
          this.$axios.post(url, this.roleData).then((res) => {
            if (res.code === 200) {
              this.$message.success(res.msg);
              // 重置数据
              this.reset();
              // 更新角色列表
              this.getRoleListAction();
            }
          });
        }
      });
    },
  },
  props: ["info"],
  data() {
    // 自定义表单校验函数
    const validMenus = (rule, value, callback) => {
      // 获取用户勾选的权限节点
      if (this.$refs.tree.getCheckedKeys().length === 0) {
        return callback(new Error("请勾选权限节点"));
      }
      callback();
    };

    return {
      // 树形控件默认需要勾选的节点
      checkedKeys: [],
      roleData: {
        rolename: "",
        menus: "",
        status: "",
      },
      // 表单校验规则
      rules: {
        rolename: {
          required: true,
          message: "请输入角色名称",
          trigger: "blur",
        },
        status: { required: true, message: "请选择角色状态", trigger: "blur" },
        menus: {
          validator: validMenus,
          trigger: "blur",
        },
      },
      defaultProps: {
        children: "children", // 指定存放子节点的属性
        label: "title", // 指定存储节点名称的属性
      },
    };
  },
  created() {
    //   请求菜单列表树形数组
    this.getMenuListAction();
  },
};
</script>

<style>
</style>